package com.yx.shop.web.controller;

import com.alibaba.druid.util.StringUtils;
import com.yx.shop.common.Const;
import com.yx.shop.entitly.PageBean;
import com.yx.shop.entitly.Product;
import com.yx.shop.service.ProductService;
import com.yx.shop.service.impl.ProductServiceImpl;
import org.junit.Test;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Classname ProductController
 * @Description TODO:
 * @Date 2021/05/07 11:39
 * @Created by yx
 */
@WebServlet("/product")
public class ProductController extends BaseServlet{
    private ProductService productService = new ProductServiceImpl();

    public String getProductByTid(HttpServletRequest request,HttpServletResponse response){
        //1.获取参数
        Integer tid = Integer.valueOf(request.getParameter("tid"));
        String pageIndex = request.getParameter("pageIndex");
        //2 判断分页的页码
        if (StringUtils.isEmpty(pageIndex)) {
            pageIndex="1";
        }
        //3.调用业务
        int count = productService.getCount(tid);
        //创建pagebean
        PageBean pageBean = new PageBean(Integer.valueOf(pageIndex),8,count);
        System.out.println(pageBean);
        //获取分页数据
        List<Product> productList = productService.getByTid(pageBean, tid);
        //存储product及page数据,并转发
        request.setAttribute("page",pageBean);
        request.setAttribute("glist",productList);
        return Const.FORWORD+"/goodsList.jsp";
    }

    public String getProductById(HttpServletRequest request, HttpServletResponse response){
        //1 获取参数
        Integer pid = Integer.valueOf(request.getParameter("pid"));
        //2 访问业务层使用主键查询数据
        Product product = productService.getProductById(pid);
        //3 获取的数据跳转页面
        request.setAttribute("goods", product);
        return Const.FORWORD+"/goodsDetail.jsp";
    }

    @Test
    public void test(){
        int count = productService.getCount(1);
        //创建pagebean
        PageBean pageBean = new PageBean(1,8,count);
        System.out.println(pageBean);
        //获取分页数据
        List<Product> productList = productService.getByTid(pageBean, 1);
        System.out.println(productList);
    }

}
